#include "ULCore_Balls.h"
#include "UL_Tools.h"
#include <iomanip>

using namespace std;

static void initBalls(vector<int> &reds, int &blue)
{
	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		reds.push_back(0);
	}

	blue = 0;
}

ULC_Balls::ULC_Balls()
{
	initBalls(reds, blue);
	outputBlue = true;
}

ULC_Balls::ULC_Balls(int red1, int red2, int red3, int red4, int red5, int red6, int blue)
{
	initBalls(reds, this->blue);
#ifdef ULC_BALLS_DEBUG
	cout << __func__ << ": argument blue - " << blue << endl;
#endif
	SetNumbers(red1, red2, red3, red4, red5, red6, blue);
	outputBlue = true;
}

ULC_Balls::ULC_Balls(int *redBalls, int blue)
{
	initBalls(reds, this->blue);
	SetNumbers(redBalls, blue);
	outputBlue = true;
}

void ULC_Balls::SetNumbers(int red1, int red2, int red3, int red4, int red5, int red6, int blue){
	reds[0] = red1;
	reds[1] = red2;
	reds[2] = red3;
	reds[3] = red4;
	reds[4] = red5;
	reds[5] = red6;
	this->blue = blue;
#ifdef ULC_BALLS_DEBUG
	cout << __func__ << "Blue - " << this->blue << " arg - " << blue << endl;
#endif
}

void ULC_Balls::SetNumbers(int *redBalls, int blue)
{
#ifdef ULC_BALLS_DEBUG
		cout << __func__ << endl;
		cout << "reds.size() = " << reds.size() << endl;
#endif
	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		reds[i] = redBalls[i];
	}

	this->blue = blue;
#ifdef ULC_BALLS_DEBUG
	cout << __func__ << "Blue - " << this->blue << " arg - " << blue << endl;
#endif

}

bool ULC_Balls::IsRedBallExisted(int number)
{
	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		if (reds[i] == number){
			return true;
		}
	}

	return false;
}


void ULC_Balls::SortRedBalls()
{
	int i, j;
	int index;
	int tmp;

	for (i = 0; i < UL_RED_BALLS_COUNT - 1; i++){
		index = i;

		for (j = i + 1; j < UL_RED_BALLS_COUNT; j++){
			if (reds[j] < reds[index]){
				index = j;
			}
		}

		tmp = reds[i];
		reds[i] = reds[index];
		reds[index] = tmp;
	}
	
}

int ULC_Balls::RedBallsSum() const
{
	int sum = 0;

	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		sum += reds[i];
	}

	return sum;
}

int ULC_Balls::TotalSum() const
{
	return RedBallsSum() + blue;
}

ULC_Balls operator+(const ULC_Balls &left, const ULC_Balls &right)
{
	ULC_Balls tmp;

	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		tmp.reds[i] = left.reds[i] + right.reds[i];
	}

	tmp.blue = left.blue + right.blue;

	return tmp;
}

bool ULC_Balls::operator==(const ULC_Balls &balls)
{
	if (blue != balls.blue){
		return false;
	}

	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		if (reds[i] != balls.reds[i]){
			return false;
		}
	}

	return true;
}

void ULC_Balls::EnableOutputBlue()
{
	outputBlue = true;
}

void ULC_Balls::DisableOutputBlue()
{
	outputBlue = false;
}

ostream& operator<<(ostream &os, const ULC_Balls &balls)
{
	for (int i = 0; i < UL_RED_BALLS_COUNT; i++){
		os.fill('0');
		os  << setw(2) << right << balls.reds[i] << " ";
	}

	if (balls.outputBlue == true){
		os.fill('0');
		os << setw(2) << right << balls.blue << endl;
	}else{
		os << endl;
	}

	os.fill(' ');

	return os;
}
